package cn.webrx.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

// /*正确  /api/*正确 /api/*.jsp(错误)  *.jsp 正确 /b.jsp正确 *.do(正确)
@WebFilter(urlPatterns = {"/api/*","/b.jsp"})
public class EncodingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        Set<String> allow = new HashSet<>(List.of("POST","PUT","PATCH"));
        HttpServletRequest req = (HttpServletRequest) request;
        if(allow.contains(req.getMethod())) {
            request.setCharacterEncoding("utf-8");
        }
        chain.doFilter(req,response);
    }
}
